package com.study.spark.scala.rdd

import org.apache.spark.{SparkConf, SparkContext}

/**
  * 累加器使用示例
  *
  * @author stephen
  * @create 2019-03-15 10:49
  * @since 1.0.0
  */
object AccumulatorDemo {

  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
      .setMaster("local[*]")
      .setAppName("Accumulator Demo")
    val sc = new SparkContext(conf)

    // 在driver端定义累计器
    val accum = sc.longAccumulator("My Accumulator")

    val rdd = sc.parallelize(List(1, 2, 3, 4, 5, 6, 7, 8, 9))

    // 在foreach种调用累加器进行对数据累加
    rdd.foreach(accum.add(_))

    println(accum.value)

    sc.stop()
  }
}
